package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootMission;
import org.footballmanager.pandafootball.system.domain.FootSj;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.service.FootBfinfoService;
import org.footballmanager.pandafootball.system.service.FootMissionService;
import org.footballmanager.pandafootball.system.service.FootSjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/hy/footMission")
public class HyMissionController extends BaseController {
	/* 实体名称 */
	private static final String ENTITYNAME = "footMission";
	private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);

	@Autowired
	private FootMissionService footMissionService;
	
	@Autowired
	private FootBfinfoService footBfinfoService;
	
	@Autowired
	private FootSjService footSjService;
	
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String openViewPage(Model map, HttpServletRequest request) {
		FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
		FootSj sj = footSjService.get(footSjService.getSj());
		FootMission mission1 = footMissionService.findMissionByUserIdandStatus(user.getId(), sj.getSjzt());
		FootMission mission2 = footMissionService.findMissionByUserIdandStatus(user.getId(), null);
		List<FootMission> missionList = new ArrayList<>();
		missionList.add(mission1);
		missionList.add(mission2);
		map.addAttribute("list", missionList);
		List<Integer> intlist = new ArrayList<>();
		//查找对应任务的完成数量
		intlist.add(footBfinfoService.countMissionNumByUserId(user.getId(), 1));
		intlist.add(footBfinfoService.countMissionNumByUserId(user.getId(), 2));
		map.addAttribute("num", intlist);
		return VIEWPAGE;
	}	

	@RequestMapping(value = "/getMissionMoney", method = RequestMethod.POST)
	@ResponseBody
	public String getMissionMoney(HttpServletRequest request, int num) {
		FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
		if(num == 1){
			footMissionService.getMissionMoney(user.getId(), false, request);
		}else{
			footMissionService.getMissionMoney(user.getId(), true, request);
		}
		FootMission mission1 = footMissionService.findMissionByUserIdandStatus(user.getId(), footSjService.get(footSjService.getSj()).getSjzt());
		FootMission mission2 = footMissionService.findMissionByUserIdandStatus(user.getId(), null);
		List<FootMission> missionlist = new ArrayList<>();
		if(null != mission1 && null != mission2){
			missionlist.add(mission1);
			missionlist.add(mission2);
			request.getSession().setAttribute("missions", missionlist);
		}else{
			request.getSession().setAttribute("missions", "");
		}
		List<Integer> intlist = new ArrayList<>();
		//查找对应任务的完成数量
		intlist.add(footBfinfoService.countMissionNumByUserId(user.getId(), 1));
		intlist.add(footBfinfoService.countMissionNumByUserId(user.getId(), 2));
		request.getSession().setAttribute("num", intlist);
		return Constants.OK;
	}	
}
